package com.czkevin.guicontrolsapi.controls;

import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.czkevin.guicontrolsapi.TupleConsumer;
import com.czkevin.guicontrolsapi.Window;

import java.util.function.Consumer;

public class IndicatorSwitchControl extends ButtonControl implements Consumer<HumanEntity> {
    public static final ItemStack LIGHT_ON;
    public static final ItemStack LIGHT_OFF;
    static {
        LIGHT_ON = new ItemStack(Material.GREEN_STAINED_GLASS_PANE);
        LIGHT_OFF = new ItemStack(Material.RED_STAINED_GLASS_PANE);
        {
            ItemMeta m = LIGHT_ON.getItemMeta();
            m.setDisplayName("\u00a77→ \u00a7a\u00a7lON");
            LIGHT_ON.setItemMeta(m);
        }
        {
            ItemMeta m = LIGHT_OFF.getItemMeta();
            m.setDisplayName("\u00a77→ \u00a7c\u00a7lOFF");
            LIGHT_OFF.setItemMeta(m);
        }
    }

    private boolean horizontal = false;

    public IndicatorSwitchControl(ItemStack icon, String label, String descriptions) {
        super(icon, label, descriptions);
        setHandler(this);
    }

    public IndicatorSwitchControl(String label, String descriptions, TupleConsumer<HumanEntity, Boolean> handlerStatus) {
        this(null, label, descriptions, false, handlerStatus);
    }

    public IndicatorSwitchControl(ItemStack icon, String label, String descriptions, TupleConsumer<HumanEntity, Boolean> handlerStatus) {
        this(icon, label, descriptions, false, handlerStatus);
    }

    public IndicatorSwitchControl(ItemStack icon, String label, String descriptions, boolean horizontal, TupleConsumer<HumanEntity, Boolean> handlerStatus) {
        super(icon, label, descriptions);
        this.horizontal = horizontal;
        setHandler(this);
        setHandlerStatus(handlerStatus);
    }

    private boolean status = false;

    private TupleConsumer<HumanEntity, Boolean> handlerStatus;

    public void setHandlerStatus(TupleConsumer<HumanEntity, Boolean> handlerStatus) {
        this.handlerStatus = handlerStatus;
    }

    @Override
    public void onClick(InventoryClickEvent event, int localX, int localY) {
        if(!(horizontal && localX == 1 && localY == 0) && !(!horizontal && localX == 0 && localY == 1)) {
            event.setCancelled(true);
            return;
        }
        super.onClick(event, localX, localY);
    }

    @Override
    public void accept(HumanEntity humanEntity) {
        status = !status;
        if(handlerStatus != null) handlerStatus.accept(humanEntity, status);
        onRender(true);
    }

    @Override
    public void onRender(boolean force) {
        Window window = getWindow();
        window.setDisplayItemAt(getX(), getY(), status ? LIGHT_ON : LIGHT_OFF);
        window.setDisplayItemAt(
            horizontal ? getX() + 1 : getX(),
            horizontal ? getY() : getY() + 1,
            getDisplayItem()
        );
    }

    @Override
    public int getWidth() {
        return horizontal ? 2 : 1;
    }

    @Override
    public int getHeight() {
        return horizontal ? 1 : 2;
    }
}
